<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	protected $params = array();
	protected $params_async = array();
	
	protected $static_files = array();
	
	
	
	public function __construct()
	{
		parent::__construct();
		
		// allow args passing after redirect
		$this->params = $this->get_last_session_params();
		$this->load_sf();
	}
	
	
	protected function set_into_params($data)
	{
		if (empty($data))
			return FALSE;
		
		$this->params = array_merge($this->params, $data);
		return TRUE;
	}
	
	protected function get_last_session_params()
	{
		$params = $this->native_session->userdata('params');
	
		if ( ! $params)
			return array();
	
		return $params;
	}
	
	protected function set_params_to_session()
	{
		$this->native_session->set_userdata('params', $this->params);
	}
	
	public function _remap($method)
	{
		$proc_method = 'proc_' . $method;
		$async_method = 'async_' . $method;
		
		$type = FALSE;
		$type = method_exists($this, $method) ? 'std' : $type;
		$type = ( ! empty($_POST) && method_exists($this, $proc_method)) ? 'proc' : $type;
		$type = method_exists($this, $async_method) ? 'async' : $type;
		
		if ( ! $type)
			show_404(strtolower(get_class($this)) . '/' . $method);
		
		if ($type == 'async')
			$this->$async_method();
		else if ($type == 'proc')
			$this->$proc_method();
		else
			$this->$method();
	}
	
	public function redirect($url, $params = array())
	{	
		if ( ! empty($params))
		{
			$this->set_into_params($params);
			$this->set_params_to_session();
		}

		redirect('/' . $url . '/');
	}
	
	
	/**
	 * render
	 *
	 * Displays a template (or returns the content)
	 */
	public function render($views, $params = array(), $return = TRUE)
	{
		//$class_name = strtolower(get_class($this));
		//$path_name = $class_name . '/' . $views;
		
		$this->load_css($views);
		$this->load_js($views);
		
		if ($return)
			return $this->load->view($views, $params, $return);
		
		$this->load->view($views, $params);
	}
	
	// load default (common) static files
	protected function _load_default_sf()
	{
		$this->static_files['css'] = array(
			'shared.css'
		);
		
		$this->static_files['less'] = array(
			'mixin.less'
		);
		
		$this->static_files['js'] = array(
			'jquery/jquery.min.js',
			'jquery/jquery-ui.min.js',
			'shared.js'
		);
	}
	
	// load controller's default static files
	protected function _load_controller_sf()
	{
		$class_name = strtolower(get_class($this));
		
		if (file_exists('./css/' . $class_name . '/' . $class_name . '.css'))
			$this->static_files['css'][] = $class_name . '/' . $class_name . '.css';
		
		if (file_exists('./less/' . $class_name . '/' . $class_name . '.less'))
			$this->static_files['less'][] = $class_name . '/' . $class_name . '.less';
		
		if (file_exists('./js/' . $class_name . '/' . $class_name . '.js'))
			$this->static_files['js'][] = $class_name . '/' . $class_name . '.js';
		
	}
	
	public function load_js($file_name)
	{
		if (empty($filename))
			return;
		
		if (file_exists('./js/' . $file_name))
			$this->static_files['js'][] = $file_name;
	}
	
	public function load_css($file_name)
	{
		if (empty($filename))
			return;
		
		if (file_exists('./css/' . $file_name))
			$this->static_files['css'][] = $file_name;
	}
	
	public function load_less($file_name)
	{
		if (empty($filename))
			return;
	
		if (file_exists('./less/' . $file_name))
			$this->static_files['less'][] = $file_name;
	}
	
	public function load_sf($sf = NULL)
	{
		$this->_load_default_sf();
		$this->_load_controller_sf();
		
		if (empty($sf))
			return;
		
		if ( ! empty($sf['css']))
			foreach($sf['css'] as $src)
				$this->load_css($src);
		
		if ( ! empty($sf['less']))
			foreach($sf['less'] as $src)
				$this->load_css($src);
		
		if ( ! empty($sf['js']))
			foreach($sf['js'] as $src)
				$this->load_js($src);
		
	}
	
	public function html($view, $params = array(), $sf = array())
	{
		// set static files
		// i.e. css, js
		// 1. build list if static files
		// 2. check if all listed item exists
		// *3. assigned time value (in hex representation)
		
		// automatically load controller's css and js files
		$this->load_sf($sf);
		
		/*
		foreach ($this->static_files['css'] as &$css)
		{
			if (file_exists('./css/' . $css))
				//$css = dechex(filemtime('./css/' . $css)) . '/' . $css;			
		}
		*/
		
		$output = $this->render('header', $this->static_files);
		
		$output .= $this->render($view, $params);
		
		$output .= $this->render('footer');
		
		echo $output;
		
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */